package com.pefis.natives;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

public class PfsBioFpJNI {
    public static final int PISFP_OK = 0;
    public static final int PISFP_FAIL = 1;
    public static final int PISFP_ERR_INVALID_CONTEXT = -101;
    public static final int PISFP_ERR_NOT_CONNECT_DEV = -102;
    public static final int PISFP_ERR_FUNC_PARAMETER = -103;
    public static final int PISFP_ERR_SYSTEM_MEMORY_ALLOC = -104;
    public static final int PISFP_ERR_TEMPLATE_ARRAY_OVER = -105;
    public static final int PISFP_ERR_CONTEXT_OVER = -106;
    public static final int PISFP_ERR_UNKNOWN = -107;
    public static final int PISFP_ERR_LOADDLL = -109;
    public static final int PISFP_ERR_INCORRECT_FPMAXCOUNT = -110;
    public static final int PISFP_ERR_DEV_STOP = -201;
    public static final int PISFP_ERR_DEV_BUSY = -202;
    public static final int PISFP_ERR_DEV_CONTROL = -203;
    public static final int PISFP_ERR_PRO_FUNC = -301;
    public static final int PISFP_ERR_ROOT_ANDROID = -401;
    public static final int PISFP_MAX_CONTEXT_COUNTS = 4;
    public static final int PISFP_MAX_DEVICE_COUNTS = 4;

    public static final int PISFP_ENROLL_TIMES = 3;
    public static final int PISFP_MIN_THRESHOLD = 1;
    public static final int PISFP_MAX_THRESHOLD = 100;
    public static final int PISFP_DEFAULT_THRESHOLD = 44;
    public static final int PISFP_MIN_ROTATION_RANGE = 45;
    public static final int PISFP_MAX_ROTATION_RANGE = 180;
    public static final int PISFP_DEFAULT_ROTATION_RANGE = 45;
    public static final int PISFP_MAX_RAW_IMAGE_RESOLUTION = 1000;
    public static final int PISFP_MIN_RAW_IMAGE_RESOLUTION = 125;
    public static final int PISFP_ALLLED = (0x00);
    public static final int PISFP_OKLED = (0x01);
    public static final int PISFP_NOLED = (0x02);
    public static final int PISFP_BKLED = (0x03);
    public static final int PISFP_LED_ON = (0x00);
    public static final int PISFP_LED_OFF = (0x01);
    public static final int PISFP_PARAM_KIND_COMPANY = 1000;
    public static final int PRODUCT_PEFIS = 0x01;
    public static final int PRODUCT_TAIWAN = 0x02;
    public static final int PRODUCT_SHANGHAI_FDT = 0x03;
    public static final int PRODUCT_COTO = 0x04;
    public static final int PRODUCT_HYSOON = 0x05;
    public static final int PRODUCT_WFS = 0x06;
    public static final String INIT_PROC = "Init";
    public static final String EXIT_PROC = "Exit";
    public static final String ENROLL_PROC = "Enroll";
    public static final String VERIFY_PROC = "Verify";
    public static final String VERIFY_FAST_PROC = "VerifyFast";
    public static final String IDENTIFY_PROC = "Identify";
    public static final String DELETE_DATA_PROC = "DeleteData";
    public static final String CAP_FP_PROC = "CaptureFp";
    public static final String ENUMERATE_DEVICE_FUNC = "pisEnumerateDevice";
    public static final String CREATE_CONTEXT_FUNC = "pisCreateContext";
    public static final String DESTROY_CONTEXT_FUNC = "pisDestroyContext";
    public static final String OPEN_DEVICE_FUNC = "pisOpenDevice";
    public static final String CLOSE_DEVICE_FUNC = "pisCloseDevice";
    public static final String GET_INFO_FUNC = "pisGetInfo";
    public static final String CAPTURE_FUNC = "pisCapture";
    public static final String CHECK_FP_FUNC = "pisCheckFp";
    public static final String PROCESS_FUNC = "pisProcess";
    public static final String PROCESSIMPORT_FUNC = "pisProcessImport";
    public static final String CREATE_TEMPLATE_FUNC = "pisCreateTemplate";
    public static final String CONVERT_TEMPLATE_FUNC = "pisConvertTemplate";
    public static final String VERIFY_FUNC = "pisVerify";
    public static final String IDENTIFY_FUNC = "pisIdentify";
    public static final String SET_MATCH_PARAMETER_FUNC = "pisSetMatchParameter";
    public static final String GET_COUNT_TPT_ARRAY_FUNC = "pisGetCountTptArray";
    public static final String ADD_TPT_ARRAY_FUNC = "pisAddTptArray";
    public static final String DELETE_TPT_ARRAY_FUNC = "pisDeleteTptArray";
    public static final String GET_TPT_ARRAY_FUNC = "pisGetAtTptArray";
    public static final String SET_TPT_ARRAY_FUNC = "pisSetAtTptArray";
    public static final String CLEAR_TPT_ARRAY_FUNC = "pisClearTptArray";
    private static final int VID = 0x0428;
    private static final int PID = 0x2600;
    private static PfsBioFpJNI mThis;

    static {
        System.loadLibrary("ast2600");
        Log.e("", "Load dll OK!");
    }

    private final Context mApplicationContext;
    private Activity m_parentAcitivity;
    private Ast2600Device mAst2600Dev;
    private boolean m_bEnumerated;
    private PfsBioFpJNI(Activity parentActivity, IUsbConnState usbConnState) {

        m_bEnumerated = false;
        Log.e("", " create on m_bEnumerated =" + m_bEnumerated);
        m_parentAcitivity = parentActivity;
        mApplicationContext = parentActivity.getApplicationContext();
        mAst2600Dev = new Ast2600Device(parentActivity, usbConnState, VID, PID);
    }

    public static PfsBioFpJNI createInstance(Activity parentActivity, IUsbConnState usbConnState) {
        if (mThis == null) {
            mThis = new PfsBioFpJNI(parentActivity, usbConnState);
        }
        return mThis;
    }

    public static synchronized PfsBioFpJNI getInstance() {

        return mThis;
    }

    public static String trimEnd(String s, String suffix) {

        if (s.endsWith(suffix)) {

            return s.substring(0, s.length() - suffix.length());

        }
        return s;
    }

    public static String ByteArrayUtf8ToString(byte[] aByteArray) {
        String strRet = "";
        try {
            strRet = new String(aByteArray, "UTF-8");//System.Encoding.GetEncoding("utf-8").GetString(aByteArray, 0, aByteArray.Length);

            strRet = strRet.trim();
        } catch (Exception ex) {
            //MessageBox.Show(ex.Message);
        }
        return strRet;
    }

    public boolean IsEnumerated() {
        Log.e("", " IsEnumerated on m_bEnumerated =" + m_bEnumerated);
        return m_bEnumerated;
    }

    public void SetEnumerate(boolean flag) {
        m_bEnumerated = flag;
        if (flag == true) {
            mAst2600Dev.m_TryConnect = 0;
        } else {
            mAst2600Dev.m_TryConnect++;
        }
    }

    public boolean IsInit() {
        return mAst2600Dev.IsInit();
    }

    public boolean InitDevice() {
        mAst2600Dev.init();

        return true;
    }

    public boolean ReInit() {
        if (mAst2600Dev.IsInit())
            mAst2600Dev.uninit();
        mAst2600Dev.init();
        return true;
    }

    public boolean CloseDevice() {
        mAst2600Dev.uninit();
        return true;
    }

    public native int pisEnumerateDevice(
            int enumerateDevID,
            byte[] devIdentifier,
            byte[] vstrDeviceDescription);

    public native int pisOpenDevice(int contextID, byte[] DevID);

    public native int pisCloseDevice(int contextID);

    public native int pisCreateContext(int[] contextID);

    public native int pisDestroyContext(int contextID);

    public native int pisGetInfo(
            int contextID,
            byte[] engineInfo,
            int[] imaWidth,
            int[] imaHeight,
            int[] imaRes,
            int[] featureSize,
            int[] templateSize);

    public native int pisCapture(int contextID, byte[] imgBuffer);

    public native int pisCheckFp(
            int contextID,
            byte[] imgBuffer,
            int imaWidth,
            int imaHeight,
            int imaRes,
            int[] isCheckFp,
            int[] fpArea);

    public native int pisProcess(
            int contextID,
            byte[] imgBuffer,
            int imaWidth,
            int imaHeight,
            int imaRes,
            byte[] featureData);

    public native int pisProcessImport(
            int contextID,
            byte[] rawImage,
            int width,
            int height,
            int res,
            byte[] featureData);

    public native int pisCreateTemplate(
            int contextID,
            byte[] featureData1,
            byte[] featureData2,
            byte[] featureData3,
            byte[] templateData);

    //===============Match
    public native int pisSetMatchParameter(int contextID,
                                           int rotationRange,
                                           int threshold);

    public native int pisVerify(int contextID,
                                byte[] featureData,
                                byte[] templateData,
                                byte[] updatedTemplateData,
                                int[] updatedFlag);

    public native int pisIdentify(int contextID,
                                  byte[] featureData,
                                  int[] identifiedTID,
                                  byte[] updatedTemplateData,
                                  int[] updatedFlag);

    //===============TptArray Management
    public native int pisGetCountTptArray(int contextID, int[] totalCounts);

    public native int pisAddTptArray(
            int contextID,
            int templateID,
            byte[] templateData);

    public native int pisDeleteAtTptArray(int contextID, int templateID);

    public native int pisGetAtTptArray(
            int contextID,
            int templateID,
            byte[] templateData);

    public native int pisSetAtTptArray(
            int contextID,
            int templateID,
            byte[] templateData);

    public native int pisClearTptArray(int contextID);

    public native int pisFpDataConvertAst2500ToAst2600(
            int contextID,
            byte[] apAst2500templateData,
            byte[] apAst2600templateData);

    public native int pisLedControl(int contextID, int ledKind, int controlValue);

    public native int pisGetDeviceInfo(int contextID, int index, int[] value);
}
